//
//  ZQTabBarController.swift
//  ZQWeibo
//
//  Created by summer的Dad on 16/11/12.
//  Copyright © 2016年 summer的Dad. All rights reserved.
//

import UIKit

class ZQTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        
        
        let tabbar = ZQTabBar()
        //不能直接赋值应该用KVC
        self.setValue(tabbar, forKey: "tabBar")
        
        
        //中间按钮的回调
        tabbar.composeClosure = {
            
            print("撰写按钮被点击了!")
            
            let composeView = ZQComposeView()
            
            composeView.showAnimation(fromVC: self)
            
            
//            UIApplication.shared.keyWindow?.addSubview(composeView)
            
        }
    
        

        addTabViewController()
        
    }
    
    //设置子控制器
    func addTabViewController () {
        
        addChildViewController(title: "首页", imageName: "tabbar_home", vc: ZQHomeTableViewController (),index:0)
        addChildViewController(title: "消息", imageName: "tabbar_message_center", vc: ZQMessageTableViewController (),index:1)
        addChildViewController(title: "发现", imageName: "tabbar_discover", vc: ZQDiscoverTableViewController (),index:2)
        addChildViewController(title: "我", imageName: "tabbar_profile", vc: ZQProfileTableViewController (),index:3)
        
    }
    
    func addChildViewController (title : String , imageName : String ,vc : UIViewController ,index : Int) {
        
    
        
        vc.title = title
        //给每一个Item附一个值
        vc.tabBarItem.tag = index
        vc.tabBarItem.image = UIImage(named:imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置字体和颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor .orange] , for: .selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont .boldSystemFont(ofSize: 10)], for: .normal)
        //设置字体距离
//        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(<#T##top: CGFloat##CGFloat#>, <#T##left: CGFloat##CGFloat#>, <#T##bottom: CGFloat##CGFloat#>, <#T##right: CGFloat##CGFloat#>)
        
        let navigation = ZQNavigationController(rootViewController: vc)
 
         self.addChildViewController(navigation)
    
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        var t = 0
    
        for subview in tabBar.subviews{
            
            if subview .isKind(of: NSClassFromString("UITabBarButton")!) {
                
                if t == item.tag {
                    
                    for v in subview.subviews {
                        
                        if v .isKind(of: NSClassFromString("UIImageView")!){
                        v.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
                        UIView.animate(withDuration: 0.1, delay: 0, options: [], animations: {
                            
                            v.transform = CGAffineTransform.identity
                            
                        }, completion: { (_) in
//                            print("OK")
                        })
                      }
                    }
      
                }
                
                t += 1
                
                
            }
    
            
        }
   
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
